package com.ruoyi.web.controller.cp;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.base.Result;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.support.Convert;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.cp.domain.Lottery;
import com.ruoyi.cp.service.ILotteryService;
import com.ruoyi.framework.web.base.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 彩票用户 控制层
 *
 * @author wh
 * @date 2019-06-25
 */
@Slf4j
@Controller
@RequestMapping("/cp/lottery")
public class LotteryController extends BaseController {
    private String prefix = "cp/lottery";

    private final ILotteryService lotteryService;

    @Autowired
    public LotteryController(ILotteryService lotteryService) {
        this.lotteryService = lotteryService;
    }

    /**
     * 列表页
     */
    @RequiresPermissions("cp:lottery:view")
    @GetMapping
    public String lottery() {
        return prefix + "/lottery";
    }

    /**
     * 新增页
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 编辑页
     */
    @GetMapping("/edit/{lotteryId}")
    public String edit(@PathVariable("lotteryId") Integer lotteryId, ModelMap mmap) {
        Lottery lottery = lotteryService.getById(lotteryId);
        mmap.put("lottery", lottery);
        return prefix + "/edit";
    }

    /**
     * 查询彩票用户列表
     */
    @RequiresPermissions("cp:lottery:list")
    @PostMapping("/list")
    @ResponseBody
    public Result list(Lottery lottery) {
        startPage();
        List<Lottery> list = lotteryService.list(lottery);
        return Result.success(getDataTable(list));
    }

    /**
     * 新增保存彩票用户
     */
    @RequiresPermissions("cp:lottery:add")
    @Log(title = "彩票用户", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public Result addSave(Lottery lottery) {
        lotteryService.save(lottery);
        return Result.success();
    }

    /**
     * 修改保存彩票用户
     */
    @RequiresPermissions("cp:lottery:edit")
    @Log(title = "彩票用户", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public Result editSave(Lottery lottery) {
        lotteryService.updateById(lottery);
        return Result.success();
    }

    /**
     * 删除彩票用户
     */
    @RequiresPermissions("cp:lottery:remove")
    @Log(title = "彩票用户", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public Result remove(String ids) {
        Long[] longIds = Convert.toLongArray(ids);
        lotteryService.removeByIds(Arrays.asList(longIds));
        return Result.success();
    }

    /**
     * 导出彩票用户列表
     */
    @RequiresPermissions("cp:lottery:export")
    @PostMapping("/export")
    @ResponseBody
    public Result export(Lottery lottery) {
        List<Lottery> list = lotteryService.list(lottery);
        ExcelUtil<Lottery> util = new ExcelUtil<>(Lottery.class);
        util.exportExcel(list, "lottery");
        return Result.success();
    }

    /**
     * 添加金币回显
     *
     * @param lotteryId
     * @param mmap
     * @return
     */
    @RequiresPermissions("cp:lottery:addVip")
    @Log(title = "添加会员", businessType = BusinessType.UPDATE)
    @GetMapping("/addVip/{lotteryId}")
    public String addVip(@PathVariable("lotteryId") Long lotteryId, ModelMap mmap) {
        Lottery lottery = lotteryService.getBaseMapper().selectById(lotteryId);
        mmap.put("lottery", lottery);
        return prefix + "/addVip";
    }

    @RequiresPermissions("cp:lottery:addVip")
    @Log(title = "添加会员", businessType = BusinessType.UPDATE)
    @PostMapping("/addVip")
    @ResponseBody
    public AjaxResult addVipSave(@RequestParam("lotteryId") Integer lotteryId,
                                 @RequestParam("vipExpire") String vipExpire) {
        System.out.println(lotteryId + "----" + vipExpire);
        try {
            lotteryService.addVip(lotteryId, vipExpire);
        } catch (CustomException e) {
            e.printStackTrace();
        }
        return AjaxResult.success();
    }
}
